package com.exmple.myapplication;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;

public class MainActivity extends AppCompatActivity {


    private boolean first = true;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.fab);
        btn.setOnClickListener(view -> {
            if (first) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fl, new SecondFragment()).commit();
                btn.setText("返回");
            } else {
                getSupportFragmentManager().beginTransaction().replace(R.id.fl, new FirstFragment()).commit();
                btn.setText("前往截图");

            }

            first = !first;
        });

        registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
            boolean isFinish = true;
            for (Map.Entry<String, Boolean> e : result.entrySet()
            ) {
                if (!e.getValue()) {
                    isFinish = false;
                    Log.i("TAG", "onActivityResult: " + e.getKey() + ":" + e.getValue());
                }
                if (!isFinish) {
                    Toast.makeText(MainActivity.this, "未获得权限", Toast.LENGTH_SHORT).show();
                }
            }
        }).launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE});
        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new FirstFragment()).commit();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
}